package com.ztools.common.util.common;

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author:一口香菜
 * @Date: 2023/3/1  21:07
 * @Version 1.0
 * 统一返回类
 */
@Data
@Builder
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 消息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;


    /**
     * 成功返回
     *
     * @return 统一返回类
     */
    public static Result success() {
        return Result.builder()
                .code(ResultCode.SUCCESS.getCode())
                .msg(ResultCode.SUCCESS.getMsg())
                .build();
    }

    /**
     * 成功返回
     *
     * @return 统一返回类
     */
    public static Result success(ResultCode resultCode) {
        return Result.builder()
                .code(resultCode.getCode())
                .msg(resultCode.getMsg())
                .build();
    }

    /**
     * 成功返回
     *
     * @return 统一返回类
     */
    public static Result success(String msg) {
        return Result.builder()
                .code(ResultCode.SUCCESS.getCode())
                .msg(msg)
                .build();
    }


    /**
     * 失败返回
     *
     * @return 统一返回类
     */
    public static Result error() {
        return Result.builder()
                .code(ResultCode.ERROR.getCode())
                .msg(ResultCode.ERROR.getMsg())
                .build();
    }

    /**
     * 失败返回
     *
     * @return 统一返回类
     */
    public static Result error(ResultCode resultCode) {
        return Result.builder()
                .code(resultCode.getCode())
                .msg(resultCode.getMsg())
                .build();
    }

    /**
     * 失败返回
     *
     * @return 统一返回类
     */
    public static Result error(String msg) {
        return Result.builder()
                .code(ResultCode.ERROR.getCode())
                .msg(msg)
                .build();
    }

    /**
     * 返回数据
     *
     * @param data 数据
     * @return 统一返回类
     */
    public Result<T> data(T data) {
        this.setData(data);
        return this;
    }

    /**
     * 返回消息
     *
     * @param msg 消息
     * @return 统一返回类
     */
    public Result<T> msg(String msg) {
        this.setMsg(msg);
        return this;
    }

    /**
     * 返回状态码
     *
     * @param code 状态码
     * @return 统一返回类
     */
    public Result<T> code(Integer code) {
        this.setCode(code);
        return this;
    }

}
